package threadbase.fiber;

import java.util.concurrent.FutureTask;

/**
 * 多线程与单线程相比较
 *
 * @author xujian
 * Creation date 2024/2/19 16:58
 * @version 1.0
 */
public class ConcurrencyTest {
    private static final long COUNT = 1000000000L;

    public static void main(String[] args) {
        concurrency();
        serial();
    }

    private static void concurrency() {
        long start = System.currentTimeMillis();

        FutureTask task= new FutureTask<>(() -> {
            long a = 0;
            for (long i = 0; i < COUNT; i++) {
                a += 5;
            }
            return a;
        });

        Thread thread2 = new Thread(task);
        thread2.start();
        long b = 0;
        for (long i = 0; i < COUNT; i++) {
            b--;
        }
        try {
            System.out.println("b=" + b + ",a=" + task.get() );
            long time = System.currentTimeMillis() - start;
            System.out.println("concurrency :" + time + "ms");
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    private static void serial() {
        long start = System.currentTimeMillis();
        long a = 0L;
        for (long i = 0; i < COUNT; i++) {
            a += 5;
        }
        long b = 0L;
        for (long i = 0; i < COUNT; i++) {
            b--;
        }
        long time = System.currentTimeMillis() - start;
        System.out.println("serial:" + time + "ms,b=" + b + ",a=" + a);
    }
}